#include "mapper.h"
#include "../../base/gmath.h"

OPEN_JLIB_NS


f32 Mapper::Linear (f32 x) noexcept {
  return x;
}


f32 Mapper::Inverse (f32 x) noexcept {
  return 1.0F - x;
}


f32 Mapper::SineIn (f32 x) noexcept {
  return sinf(IEEE754<f32>::PI_2 * x);
}


f32 Mapper::SineOut (f32 x) noexcept {
  return 1.0F - cosf(IEEE754<f32>::PI_2 * x);
}


f32 Mapper::SineInOut (f32 x) noexcept {
  return 0.5F - cosf(IEEE754<f32>::PI * x) * 0.5F;
}

f32 Mapper::SineOutIn (f32 x) noexcept {
  return x <= 0.5F ? sinf(IEEE754<f32>::PI * x) * 0.5F : 1.0F - cosf((x - 0.5F) * IEEE754<f32>::PI) * 0.5F;
}



f32 Mapper::QuadIn (f32 x) noexcept {
  return x * x;
}

f32 Mapper::QuadOut (f32 x) noexcept {
  return (2.0F - x) * x;
}

f32 Mapper::QuadInOut (f32 x) noexcept {
  return x > 0.5F ? 2.0F * x * (1.0F - x) : 2.0F * x * x;
}

f32 Mapper::QuadOutIn (f32 x) noexcept {
  return x > 0.5F ? 2.0F * x * (x - 1.0F) + 1.0F : 2.0F * x * (1.0F - x);
}



f32 Mapper::CubicIn (f32 x) noexcept {
  return x * x * x;
}

f32 Mapper::CubicOut (f32 x) noexcept {
  return (x - 1.0F) * (x - 1.0F) * (x - 1.0F) + 1.0F;
}

f32 Mapper::CubicInOut (f32 x) noexcept {
  return x > 0.5F ? 4.0F * (x - 1.0F) * (x - 1.0F) * (x - 1.0F) + 1.0F : 4.0F * x * x * x;
}

f32 Mapper::CubicOutIn (f32 x) noexcept {
  return x > 0.5F ? 4.0F * (x - 0.5F) * (x - 0.5F) * (x - 0.5F) + 0.5F : 0.5F + (x - 0.5F) * (x - 0.5F) * (x - 0.5F);
}



f32 Mapper::CircleIn (f32 x) noexcept {
  return 1.0F - sqrtf(1.0F - x * x);
}

f32 Mapper::CircleOut (f32 x) noexcept {
  return sqrtf(1.0F - (1.0F - x) * (1.0F - x));
}

f32 Mapper::CircleInOut (f32 x) noexcept {
  return x > 0.5F ? sqrtf(1.0F - (2.0F - x - x) * (2.0F - x - x)) * 0.5F + 0.5F : 0.5F - sqrtf(1.0F - 4.0F * x * x) * 0.5F;
}

f32 Mapper::CircleOutIn (f32 x) noexcept {
  return x > 0.5F ? 1.0F - sqrtf(1.0F - (x + x - 1.0F) * (x + x - 1.0F)) * 0.5F : sqrtf(4.0F * x * (1.0F - x)) * 0.5F;
}



f32 Mapper::BackIn (f32 x) noexcept {
  constexpr f32 factor = 1.70158F;
  return x * x * ((factor + 1.0F) * x - factor);
}

f32 Mapper::BackOut (f32 x) noexcept {
  constexpr f32 factor = 1.70158F;
  x -= 1.0F;
  return x * x * ((factor + 1.0F) * x + factor) + 1.0F;
}

f32 Mapper::BackInOut (f32 x) noexcept {
  constexpr f32 factor = 1.70158F * 1.525F;
  x += x;
  if (x < 1.0F)
    return x * x * ((factor + 1.0F) * x - factor) * 0.5F;
  x -= 2.0F;
  return x * x * ((factor + 1.0F) * x + factor) * 0.5F + 1.0F;
}

f32 Mapper::BackOutIn (f32 x) noexcept {
  constexpr f32 factor = 1.70158F * 1.525F;
  x += x - 1.0F;
  if (x < 1.0F)
    return (x * x * ((factor + 1.0F) * x + factor) + 1.0F) * 0.5F;
  return (x * x * ((factor + 1.0F) * x - factor)) * 0.5F + 0.5F;
}


CLOSE_JLIB_NS

